<?php

class Auth extends MY_Controller {

    public function __construct() {
        parent::__construct();
    }

    public function login() {
        $password = $this->input->post('password');

        if ($password == false) {
            $this->load->view('backend/auth_login_view');
        } else {
            $username = trim($this->input->post('username'));

            $this->load->model('user_model');

            $user = $this->user_model->find(array(array('username', $username)));

            if ($user && ($user->password == md5($password))) {
                $this->session->set_userdata(array(
                    'g_user_id' => $user->id,
                    'g_user_name' => $user->username
                ));
                $this->json_back('XT001','登录成功');
            } else {
                $this->json_back('XT002', '您的帐号或密码错误');
            }
        }
    }

    public function logout() {
        $this->session->unset_userdata('g_user_id');

        $this->session->sess_destroy();

        $this->load->helper('url');

	$login_url = SITE_URL.'/index.php/backend/auth/login';
	header("Location: {$login_url}");
        exie(0);

    }

}